package com.hk.huakangcompany.commons.security;


import com.hk.huakangcompany.commons.ResultCodeEnum;
import com.hk.huakangcompany.model.vo.Result;
import com.hk.huakangcompany.model.vo.ResultVO;
import com.hk.huakangcompany.model.vo.UploadImage;
import com.hk.huakangcompany.model.vo.UploadImageVO;

import java.util.List;

/**
 * 通用返回对象
 * 2020/7/6
 */
public class CommonResult<T> {
    private long code;
    private String message;
    private T data;

    protected CommonResult() {
    }

    protected CommonResult(long code, String message, T data) {
        this.code = code;
        this.message = message;
        this.data = data;
    }

    /**
     * 成功返回结果
     *
     * @param data 获取的数据
     */
    public static <T> CommonResult<T> success(T data) {
        return new CommonResult<T>(ResultCode.SUCCESS.getCode(), ResultCode.SUCCESS.getMessage(), data);
    }

    /**
     * 成功返回结果
     *
     * @param data 获取的数据
     * @param  message 提示信息
     */
    public static <T> CommonResult<T> success(T data, String message) {
        return new CommonResult<T>(ResultCode.SUCCESS.getCode(), message, data);
    }

    /**
     * 失败返回结果
     * @param errorCode 错误码
     */
    public static <T> CommonResult<T> failed(IErrorCode errorCode) {
        return new CommonResult<T>(errorCode.getCode(), errorCode.getMessage(), null);
    }

    /**
     * 失败返回结果
     * @param message 提示信息
     */
    public static <T> CommonResult<T> failed(String message) {
        return new CommonResult<T>(ResultCode.FAILED.getCode(), message, null);
    }

    /**
     * 失败返回结果
     * @param validateFailed
     */
    public static <T> CommonResult<T> failed(ResultCode validateFailed) {
        return failed(ResultCode.FAILED);
    }

    /**
     * 参数验证失败返回结果
     */
    public static <T> CommonResult<T> validateFailed() {
        return failed(ResultCode.VALIDATE_FAILED);
    }

    /**
     * 参数验证失败返回结果
     * @param message 提示信息
     */
    public static <T> CommonResult<T> validateFailed(String message) {
        return new CommonResult<T>(ResultCode.VALIDATE_FAILED.getCode(), message, null);
    }

    /**
     * 未登录返回结果
     */
    public static <T> CommonResult<T> unauthorized(T data) {
        return new CommonResult<T>(ResultCode.UNAUTHORIZED.getCode(), ResultCode.UNAUTHORIZED.getMessage(), data);
    }

    /**
     * 未授权返回结果
     */
    public static <T> CommonResult<T> forbidden(T data) {
        return new CommonResult<T>(ResultCode.FORBIDDEN.getCode(), ResultCode.FORBIDDEN.getMessage(), data);
    }

//  自己定义的返回结果
    /**
     * Layui数据表格格式查询获取的值为空时
     * @param count
     * @param list
     * @return
     */
    public static ResultVO nullResultVO(long count, List list) {
        ResultVO resultVO = new ResultVO<>(ResultCodeEnum.NULL.getCode(), ResultCodeEnum.NULL.getMessage(),count,list);
        return resultVO;
    }

    /**
     * Layui数据表格格式查询
     * 未授权返回结果
     */
    public static ResultVO commonResultVO(Integer count, List list) {
        ResultVO resultVO = new ResultVO<>(ResultCodeEnum.FORBIDDEN.getCode(), ResultCodeEnum.FORBIDDEN.getMessage(),count,list);
        return resultVO;
    }



    /**
     * Layui数据表格格式成功时
     * @param count
     * @param list
     * @return
     */
    public static ResultVO successResultVO(long count,List list) {
        ResultVO resultVO = new ResultVO<>(ResultCodeEnum.SUCCESS.getCode(), ResultCodeEnum.SUCCESS.getMessage(),count,list);
        return resultVO;
    }

    /**
     * 查询获取的值为空时
     * @param o
     * @return
     */
    public static Result nullResult(Object o) {
        Result result = new Result(ResultCodeEnum.NULL.getCode(), ResultCodeEnum.NULL.getMessage(),o);
        return result;
    }

    /**
     * 成功时
     * @param o
     * @return
     */
    public static Result successResult(Object o) {
        Result result = new Result(ResultCodeEnum.SUCCESS.getCode(), ResultCodeEnum.SUCCESS.getMessage(),o);
        return result;
    }

    /**
     * 失败时
     * @param o
     * @return
     */
    public static Result errorResult(Object o) {
        Result result = new Result(ResultCodeEnum.ERROR.getCode(), ResultCodeEnum.ERROR.getMessage(),o);
        return result;
    }

    /**
     *  未登录
     * @param o
     * @return
     */
    public static Result acResult(Object o) {
        Result result = new Result(ResultCodeEnum.AC.getCode(), ResultCodeEnum.AC.getMessage(),o);
        return result;
    }

    /**
     * 上传文件格式成功时
     * @param o
     * @return
     */
    public static UploadImageVO successUploadImgVO(UploadImage o) {
        UploadImageVO uploadImageVO = new UploadImageVO(ResultCodeEnum.IMGSUCCESS.getCode(), ResultCodeEnum.SUCCESS.getMessage(),o);
        return uploadImageVO;
    }

    /**
     * 上传文件格式失败时
     * @param o
     * @return
     */
    public static UploadImageVO errorUploadImgVO(UploadImage o) {
        UploadImageVO uploadImageVO = new UploadImageVO(ResultCodeEnum.ERROR.getCode(), ResultCodeEnum.ERROR.getMessage(),o);
        return uploadImageVO;
    }

    public long getCode() {
        return code;
    }

    public void setCode(long code) {
        this.code = code;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }
}